*rwmutex) Runlock ()
Func (rw *rwmutex) Unlock ()
Where the mutex is a mutex, lock () lock, Unlock () Unlock, use lock () lock, you can not lock it again, until it is unlocked with Unlock (), it can be locked again. For read-write uncertainty scenarios, where there is no obvious difference between read and write times, and only one read or write is allowed, the lock leaf is called a global lock.
Func (M *mutex) Unlock () is used to unlock m, whi
This is a creation in
Article, where the information may have evolved or changed. The Golang Sync package implements two types of lock mutexes (mutexes) and Rwmutex (read and write locks), where Rwmutex is implemented based on mutexes, and read-only locks are implemented using similar functions as reference counters.
Type Mutex
func (M *mutex) Lock ()
func (M *mutex) Unlock ()
type Rwmutex
func (rw *rwmutex) Lock ()
func (rw *rwmutex
not have a task and are in the waiting state to get the task from blockingqueue. But cannot interrupt those in the taskThe thread in the execution process, or the thread suspended during the execution of the task. Check the implementation code to find out the cause:Java code
VoidInterruptifidle (){
FinalReentrantlock runlock = This. Runlock;
If([B] runlock.
:25:36 runsync pos=02016-04-20 11:25:36 runlock pos=02016-04-20 11:25:37 runLock pos=12016-04-20 11:25:38 runlock pos=22016-04-20 11:25:39 runlock pos=32016-04-20 11:25:40 runlock pos=4As can be seen from the output, in the anonymous inner class, the lock operation of synchr
when there is a write lock. Func (rw *rwmutex) Unlock (): Write lock unlock, which causes a run-time error if no write lock is made. Func (rw *rwmutex) Rlock (): Read lock, when there is a write lock, cannot load read lock, when there is only read lock or no lock, can load read lock, read lock can load multiple, so it is suitable for "read more write less" scenario. Func (rw *rwmutex) Runlock (): Read lock unlock,
class worker implements runnable {/*** runs a single task between before/after methods. */private void runtask (runnable task) {final reentrantlock runlock = This. runlock; runlock. lock (); try {/** if pool is stopping ensure thread is interrupted; * If not, ensure thread is not interrupted. this requires * a double-check of State in case the interrupt was * cl
that executes the task
if (t!= null) {
w.thread = t; The member Variable
workers.add (w) that assigns the reference of the thread being created to W;
int nt = ++poolsize; Current number of threads plus 1
if (NT > Largestpoolsize)
largestpoolsize = NT;
}
return t;
}
In the Addthread method, you first create a worker object with the submitted task, and then call the thread factory threadfactory create a new t
, you can guarantee that only the lock can be unlocked.Here is the test code:
123456789101112131415161718192021st22232425262728293031323334353637
public class Testdlock {public static void Main (string[] args) throws Exception {Rmiserver rmiserver = new Localrmiserver ();Distributedlockimpl Distributedlock = new Distributedlockimpl ();Rmiserver.registerremoteobject ("Lock1", Distributedlock);Multithreadprocessor processor = new Multithreadprocessor ("AA");for (int i = 0; i Proce
Before you look at whatever code you want, first think about which parts of the thread pool should be:
Task Queue
Thread
Task queue is very good, directly with the blocking queue can be:blockingqueueprivate final class Worker implements Runnable {private final Reentrantlock Runlock = new Re Entrantlock ();p rivate Runnable firsttask;volatile long completedtasks; Thread thread;//runs taskprivate void RunTask (Runnable Task) {final Ree
Before you look at whatever code you want, first think about which parts of the thread pool should be:
Task Queue
Thread
The task queue is very good. The:blockingqueueprivate final class Worker implements Runnable {private final Reentrantlock Runlock = new Re Entrantlock ();p rivate Runnable firsttask;volatile long completedtasks; Thread thread;//runs taskprivate void RunTask (Runnable Task) {final Reentrantlock
(interface{}), which is a function type, the parameter of the function type is a interface{ }, which is an empty interface, because any type can be considered to implement an empty interface, so you can receive any type of argument here. That is, the type of f is a function type that can receive any type of argument. A bit around, need to calm down to understand oh ~
The source code is as follows:
1//keepalivemarksanitemtobekeptforanother Expiredurationperiod. 2//"Update Accessedon to Current
, use mutual exclusion locks:
var// guards icons// Concurrency-safe.funcstring) image.Image { mu.Lock() defer mu.Unlock() ifnil { loadIcons() } return icons[name]}
However, the problem is that after icons is initialized, subsequent read operations cannot be performed in parallel.
Solution two use a read-write lock
var mu sync. Rwmutex //guards icons var icons map [string ]image. Image//concurrency-safe. func Icon (name string ) image. Image {mu. Rlock () if icons! =
(*rwmutex) Rlock
Func (*rwmutex) Runlock
The name and signature of the first pair of methods are exactly the same as the two methods of the mutex. They represent the lock and unlock of the write operation respectively. For short, they are write locks and write unlocks. The latter pair of methods represent the lock and unlock of the read operation respectively. Hereinafter referred to as read lock and read unlock.
Writing locks on read-wr
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.